﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.StorageClient;
using Microsoft.WindowsAzure.ServiceRuntime;

namespace FtpWorkerRole.AzureStorage
{
    public class AzureUser
    {
        //private static string accountStr = "DefaultEndpointsProtocol=https;AccountName=firstblooddata;AccountKey=mgWEj3mTZekLK+79Q2HXC8ckw0rmAP7GD4E6ndilb/lMiCs00JxB983s9T6v8S/ApSi0b5OF35Uayg7M1v3vDA==";
        public static bool login(string username, string password)
        {
            //var storageAcc = CloudStorageAccount.DevelopmentStorageAccount;
            var storageAcc = CloudStorageAccount.Parse(
                  RoleEnvironment.GetConfigurationSettingValue(
                  "Microsoft.WindowsAzure.Plugins.Diagnostics.ConnectionString"));
            var tableClient = storageAcc.CreateCloudTableClient();

            var context = new UserContext(storageAcc);

            var query = context.UserEntities.Where(
                u => u.Name == username).ToList();
            if (query.Count == 0)
                return false;
            var result = query.FirstOrDefault();
            return result.Password == password;
            
        }
        public static string isShared(string username)
        {
            var storageAcc = CloudStorageAccount.Parse(
                  RoleEnvironment.GetConfigurationSettingValue(
                  "Microsoft.WindowsAzure.Plugins.Diagnostics.ConnectionString"));
            var tableClient = storageAcc.CreateCloudTableClient();

            var context = new UserContext(storageAcc);

            var query = context.UserEntities.Where(
                u => u.Name == username).ToList();
            var result = query.FirstOrDefault();
            if (result.IsShared)
            {
                return "share";
            }
            else
            {
                return username;
            }
        }
    }
    public class UserContext : TableServiceContext
    {
        private CloudStorageAccount _account;
        private readonly string _tableName = "User";

        public UserContext(CloudStorageAccount account)
            : base(account.TableEndpoint.AbsoluteUri, account.Credentials)
        {
            _account = account;

            var client = new CloudTableClient(_account.TableEndpoint.AbsoluteUri, _account.Credentials);
            client.CreateTableIfNotExist(_tableName);
        }

        public IQueryable<UserEntity> UserEntities
        {
            get
            {
                return CreateQuery<UserEntity>(_tableName);
            }
        }

        public void AddUser(UserEntity user)
        {
            AddObject(_tableName, user);
            SaveChanges();
        }

        public void DeleteUser(UserEntity user)
        {
            DeleteObject(user);
            SaveChanges();
        }

        public void UpdateUser(UserEntity user)
        {
            UpdateObject(user);
            SaveChanges();
        }
    }
    public class UserEntity : TableServiceEntity
    {
        public string Name { get; set; }

        public string Password { get; set; }

        public bool IsShared { get; set; }

        public UserEntity(string name)
            : base(name, Guid.NewGuid().ToString())
        {
        }

        public UserEntity()
            : base()
        {
        }
    }
}
